#include <stdio.h>
#include "cv.h"
#include "highgui.h"

IplImage *img0, *img1;

int window_width_ = 500;
int window_height_ = 375;
int padding_ = 15;
int left_max_ = 0 + padding_;
int right_max_ = window_width_ - padding_;
int left_zero_ = window_width_ / 2 - padding_;
int right_zero_ = window_width_ / 2 + padding_;
float angular_ = 0.0f;

float linear_ = 0.0f;

void mouseHandler(int event, int x, int y, int flags, void *param)
{

    switch(event) {

        /* left button down */

        case CV_EVENT_LBUTTONDOWN:

            //faire avancer le robot
	    	linear_ = 0.5f;

            break;

        /* right button down */

        case CV_EVENT_RBUTTONDOWN:

            //faire reculer le robot
			linear_ = -0.5f;

            break;

     /* mouse move */

        case CV_EVENT_MOUSEMOVE:

            //calcul de la vitesse de rotation par rapport à la position de la souris
			//

			//souris au centre
			if(x >= left_zero_ && x<= right_zero_)
			{
				angular_ = 0.0f;
			}

			//souris au mayimum à droit ou à gauche
			else if(x < left_max_ || x > right_max_)
			{
				if(x < left_max_)
				{
					//gauche
					angular_ = 1.5f;
				}
				else
				{
					//droite
					angular_ = -1.5f;
				}
			}

			//souris côté gauche entre le centre et le may
			else if(x>=left_max_ && x<left_zero_)
			{
				angular_ = (1.0f - (((x - left_max_)*1.0f) / ((left_zero_ - left_max_)*1.0f))) *1.5f;
			}

			//souris côté droit entre le centre et le may
			else if(x<right_max_ && x>=right_zero_)
			{
				angular_ = (((x - right_zero_)*1.0f) / ((right_max_ - right_zero_)*1.0f)) *-1.5f;
			}

			printf("linear_ : %f     angular_ : %f\n", linear_, angular_);
            break;

    }

}

int main(int argc, char** argv)
{
	printf("width : %d\n", window_width_);
	printf("height : %d\n", window_height_);
	printf("left max : %d\n", left_max_);
	printf("right max : %d\n", right_max_);
	printf("left zero : %d\n", left_zero_);
	printf("right zero : %d\n", right_zero_);

    /* load an image */
    img0 = cvLoadImage("2.jpg", CV_LOAD_IMAGE_COLOR);
   
    /* create new window and register mouse handler */
    cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
    cvSetMouseCallback( "image", mouseHandler, NULL );
   
    /* display the image */
    cvShowImage("image", img0);
    cvWaitKey(0);
   
    cvDestroyWindow("image");
    cvReleaseImage(&img0);
   
    return 0;
}
 


